<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use think\Db;

class Event extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';



    public function getList() {
        $page = $this->request->get('page', 1);
        $limit = $this->request->get('limit', 10);
        $school_id = $this->request->get('school_id');
        $level1 = $this->request->get('level1');
        $level2 = $this->request->get('level2');
        $latitude = $this->request->get('latitude');
        $longitude = $this->request->get('longitude');
        if($latitude==""){
            $latitude=22.81000000000000;
        }
        if($longitude==""){
            $longitude=108.35528564453125;
        }
        $keyword = $this->request->get('keyword');
        $order = $this->request->get('order', 'e.id desc');


        $where = [];
        if ($school_id) {
            $where[] = ['s.id', '=', $school_id];
        }
        if ($level1) {
            $where[] = ['sbj.id', '=', $level1];
        }
        if ($level2) {
            $where[] = ['sbj.id', '=', $level2];
        }
        if ($keyword) {
            $where[] = ['c.name|c.intro|c.labels|course.title', 'like', "%$keyword%"];
        }

        $list = Db::name('course_table_autoplan_temp')
            ->alias('t')
            ->join('fa_course_table tb', 'tb.id=t.table_id', 'INNER')
            ->join('eb_school s', 'tb.school_id=s.id', 'INNER')
            ->join('eb_classes c', 'c.id=t.classes_id', 'INNER')
            ->join('fa_ocation o', 'o.id=t.ocation_id', 'INNER')
            ->join('fa_ocation o2', 'o.pid=o2.id', 'INNER')
            ->field("c.id,s.name as school_name,o2.name as ocation_name,c.name as className,c.cover,subjectlv_id_b,c.price,tb.start_date,o.`longitude`,o.`latitude`, (
        6371 * ACOS(
            COS(RADIANS($latitude)) * COS(RADIANS(o.latitude)) * COS(RADIANS(o.longitude) - RADIANS($longitude)) +
            SIN(RADIANS($latitude)) * SIN(RADIANS(o.latitude))
        )
    ) AS km")
            ->group("t.ocation_id,t.classes_id")
            ->order("km asc")
            ->page($page, $limit)
            ->select();

        foreach ($list as $key => $value) {
            $list[$key]['cover'] = preg_replace('/^https?\:\/\/.*\.myqcloud\.com\//', 'https://tcimg.gxyushengwenhua.com/', $value['cover']);
        }


        $total = Db::name('course_table_autoplan_temp')
            ->alias('e')
        ->find();

        if (!$list) {
            $this->error('数据不存在');
        }
        $this->success('获取成功', $list);
    }


    /**
     * 获取活动详情
     */
    public function getInfo()
    {
        $id = $this->request->get('id');
        if (!$id) {
            $this->error('参数错误');
        }

        $info = Db::name('course_table_autoplan_temp')
            ->alias('e')
            ->join('eb_classes c', 'e.classes_id = c.id', 'LEFT')
            ->join('eb_school s', 'c.school_id = s.id', 'LEFT')
            ->join('eb_course course', 'e.table_id = course.id', 'LEFT')
            ->join('eb_class_teacher cst', 'cst.classes_id = c.id', 'LEFT')
            ->join('subject_lv sbj', 'sbj.id = c.subject_lv', 'LEFT')
            ->field('e.*, c.name as class_name, cst.teacher_id as teacher_name, 
                    s.name, course.title as course_name, c.price, 
                    sbj.name as level1, sbj.name as level2, c.labels as tags, 
                    c.intro content, c.num max_students, c.num current_students,
                    course.start_date start_time, c.address')
            ->where('e.id', $id)
            ->find();

        if (!$info) {
            $this->error('数据不存在');
        }

        $this->success('获取成功', $info);
    }
}